package com.lss.loginregister.util;

import static android.content.Context.MODE_PRIVATE;

import android.content.Context;
import android.content.SharedPreferences;

public class PrefsUtil {
    private static final String PREFS_NAME = "login_prefs";
    private static final long EXPIRATION_TIME = 7 * 24 * 60 * 60;

    public static void saveCredentials(Context context, String username, String password) {
        SharedPreferences.Editor editor = context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE).edit();
        editor.putString("username", username);
        editor.putString("password", password);
        editor.apply();
    }

    public static String[] getCredentials(Context context) {
        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        return new String[] {
            prefs.getString("username", ""),
            prefs.getString("password", "")
        };
    }

    public static void clearCredentials(Context context) {
        context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE).edit().clear().apply();
    }

    private static final String KEY_LAST_LOGIN_TIME = "last_login_time";

    public static void updateLoginTime(Context context) {
        SharedPreferences.Editor editor = context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE).edit();
        editor.putLong(KEY_LAST_LOGIN_TIME, System.currentTimeMillis());
        editor.apply();
    }

    public static boolean isLoginExpired(Context context) {
        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        long lastLoginTime = prefs.getLong(KEY_LAST_LOGIN_TIME, 0);
        return System.currentTimeMillis() - lastLoginTime > EXPIRATION_TIME;
    }
}